package org.wedding.center.controller;

import cn.dev33.satoken.stp.StpUtil;
import org.wedding.center.dto.query.OrderQuery;
import org.wedding.center.entity.OrderInfo;
import org.wedding.center.service.OrderInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.wedding.utils.CommonResult;
import org.wedding.dto.PageModel;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 订单信息表(OrderInfo)表控制层
 *
 * @author songkaiyue
 * @since 2025-05-14 16:30:24
 */
@AllArgsConstructor
@RestController
@RequestMapping("/orderInfo")
@Tag(name = "OrderInfo", description = "订单信息表")
public class OrderInfoController {
    private final OrderInfoService orderInfoService;


    /**
     * 分页查询所有数据
     *
     * @param query 分页对象
     * @return 所有数据
     */
    @Operation(summary = "分页查询所有数据")
    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult<PageModel> list(OrderQuery query) {
        return orderInfoService.findAll(query);
    }

    @Operation(summary = "分页查询所有数据")
    @RequestMapping(value = "/listuser", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult<PageModel> listuser(OrderQuery query) {
        return orderInfoService.findAllUser(query);
    }



    /**
     * 分页查询所有数据
     *
     * @param query 分页对象
     * @return 所有数据
     */
    @Operation(summary = "分页查询所有数据")
    @RequestMapping(value = "/listSystem", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult<PageModel> listSystem(OrderQuery query) {
//        String role = StpUtil.getSession().get("role").toString();
//        System.out.println(role);
//        String userId = StpUtil.getSession().get("userId").toString();
//        if("1".equals(role)) {
//            //若是商家则查询自己的订单
//            query.setUserId(userId);
//        }
        return orderInfoService.findAll(query);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过主键查询单条数据")
    @RequestMapping(value = "/view", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult view(String id) {
        return orderInfoService.view(id);
    }
    @RequestMapping(value = "/findByCode", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult findByCode(String code) {
        OrderInfo byCode = orderInfoService.findByCode(code);
        return CommonResult.success(byCode);
    }



    /**
     * 通过核销码查询数据
     *
     * @param redeemCode
     * @return 单条数据
     */
    @Operation(summary = "通过核销码查询数据")
    @RequestMapping(value = "/findByRedeemCode", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult findByRedeemCode(String redeemCode) {
        return orderInfoService.findByRedeemCode(redeemCode);
    }

    /**
     * 新增数据
     *
     * @param orderInfo 实体对象
     * @return 新增结果
     */
    @Operation(summary = "新增数据")
    @RequestMapping(value = "/add", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult add(@RequestBody OrderInfo orderInfo) {
        return orderInfoService.add(orderInfo);
    }

    /**
     * 修改数据
     *
     * @param orderInfo 实体对象
     * @return 修改结果
     */
    @Operation(summary = "修改数据")
    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult update(@RequestBody OrderInfo orderInfo) {
        return orderInfoService.updateEntity(orderInfo);
    }

    /**
     * 删除数据
     *
     * @param ids 主键
     * @return 删除结果
     */
    @Operation(summary = "删除数据")
    @RequestMapping(value = "/delete", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult delete(@RequestBody List<String> ids) {
        return orderInfoService.delete(ids);
    }

    /**
     * 获取预约信息
     *
     * @param type
     * @return
     */
    @Operation(summary = "获取预约信息")
    @RequestMapping(value = "/getAppointment", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult getAppointment(String type) {
        return orderInfoService.getAppointment(type);
    }

    /**
     * 核销订单
     *
     * @param redeemCode
     * @return
     */
    @Operation(summary = "核销订单")
    @RequestMapping(value = "/redeem", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json; charset=UTF-8")
    public CommonResult redeem(String redeemCode) {
        return orderInfoService.redeem(redeemCode);
    }


}

